package com.buleqr.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buleqr.dao.CouponCodeDao;
import com.buleqr.dto.request.AddCouponCodeReq;
import com.buleqr.dto.request.CouponQuery;
import com.buleqr.dto.response.CouponCodeVo;
import com.buleqr.pojo.AjaxResult;
import com.buleqr.pojo.CouponCode;
import com.buleqr.pojo.PageList;
import com.buleqr.service.ICouponCodeService;
import lombok.AllArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class CouponCodeServiceImpl implements ICouponCodeService {

    private final CouponCodeDao baseDao;
    private final ModelMapper modelMapper;

    @Override
    public AjaxResult batchCreate(AddCouponCodeReq req) {
        if (Objects.isNull(req.getStartTime()) || Objects.isNull(req.getEndTime())) {
            return AjaxResult.error("参数错误");
        }
        if (req.getStartTime().isBefore(LocalDateTime.now())) return AjaxResult.error("开始时间不能小于当前时间");
        if (req.getStartTime().isAfter(req.getEndTime())) return AjaxResult.error("开始时间不能大于结束时间");

        List<CouponCode> list = new ArrayList<>();
        for (int i = 0;i<100;i++) {
            CouponCode couponCode = new CouponCode();
            couponCode.setStartTime(req.getStartTime());
            couponCode.setEndTime(req.getEndTime());
            couponCode.setStatus(0);
            couponCode.setName("券码");
            couponCode.setCreateTime(LocalDateTime.now());
            couponCode.setUpdateTime(LocalDateTime.now());
            couponCode.setCode(RandomUtil.randomString(8));
            couponCode.setCreatorId(0L);
            list.add(couponCode);
        }
        baseDao.saveBatch(list);
        return AjaxResult.success("生成成功，共100条");
    }

    @Override
    public PageList<CouponCodeVo> list(CouponQuery query) {
        LambdaQueryChainWrapper<CouponCode> lambdaQuery = baseDao.lambdaQuery();
        lambdaQuery.eq(CouponCode::getStatus,0);
        if (query.getCode()!=null) {
            lambdaQuery.eq(CouponCode::getCode,query.getCode());
        }
        if (query.getOrderByColumn()!=null&&query.getOrderByColumn().equals("createTime")) {
            if (query.getIsAsc()!=null&&query.getIsAsc().equals("asc")) {
                lambdaQuery.orderByAsc(CouponCode::getCreateTime);
            }else {
                lambdaQuery.orderByDesc(CouponCode::getCreateTime);
            }
        }
        Page<CouponCode> page = lambdaQuery.page(new Page<>(query.getPageNum(), query.getPageSize()));
        List<CouponCodeVo> rowVos = new LinkedList<>();
        page.getRecords().forEach((item) -> {
            CouponCodeVo rowVo = modelMapper.map(item, CouponCodeVo.class);
            rowVos.add(rowVo);
        });
        return PageList.of(rowVos,page);
    }
}
